package com.yunduansing.core.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.config.constant.SysConstant;
import com.yunduansing.core.dao.redis.JedisClient;
import com.yunduansing.core.dto.MenuDetailDto;
import com.yunduansing.core.dto.MenuSearch;
import com.yunduansing.core.model.ButtonModel;
import com.yunduansing.core.model.MenuModel;
import com.yunduansing.core.service.MenuService;
import com.yunduansing.core.service.MenuTypeService;
import com.yunduansing.core.service.TemplateService;
import com.yunduansing.core.utils.CommonUtil;

@Controller
@RequestMapping("/manager/menu")
public class MenuController {
	@Autowired
	private MenuService service;
	@Autowired
	private JedisClient jedisClient;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/menu/index");
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/page",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String pagination(DbCommon.Grid grid,@RequestParam(value="search",required=false) 
		MenuSearch search){
		return CommonUtil.toJsonWithGson(service.pagination(grid, search));
	}
	
	@RequestMapping(value="/detail")
	public ModelAndView detail(Model m,@RequestParam(value="id",required=false) Integer id){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/menu/detail");
		MenuDetailDto model;
		if(id!=null)
			model=service.getModelAndParentById(id);
		else
			model=new MenuDetailDto();
		m.addAttribute("model", model);
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String delete(@RequestParam(value="ids[]") List<Object> ids){
		return CommonUtil.toJsonWithGson(service.deleteByKeys(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="/getMenuJson",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String getMenuJson(@RequestParam(value="id",required=false) Integer id,boolean showButton){
		return service.getMenuJson(showButton);
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String save(MenuModel model){
		return CommonUtil.toJsonWithGson(service.save(model));
	}
	
	@RequestMapping("/button")
	public String button(Model m,int menuId){
		m.addAttribute("btnList", service.getButtonsByMenuId(menuId));
		return "/beetl/menu/button";
	}
	@ResponseBody
	@RequestMapping(value="/saveButton",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String saveButton(ButtonModel model){
		return CommonUtil.toJsonWithGson(service.saveButton(model));
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteButtonById",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String deleteButtonById(int buttonId){
		return CommonUtil.toJsonWithGson(service.deleteButtonById(buttonId));
	}
}
